package cn.com.cifi.mars.service.impl;


import cn.com.cifi.mars.bean.vo.sys.req.SaveSysPositionGroupAuthorizeReqParam;
import cn.com.cifi.mars.entity.common.SysPositionGroupAuthorize;
import cn.com.cifi.mars.mapper.mars.SysPositionGroupAuthorizeMapper;
import cn.com.cifi.mars.service.SysPositionGroupAuthorizeService;
import cn.com.cifi.mars.util.UUIDUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Iterator;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 岗位组自动授权关联表 服务实现类
 * </p>
 *
 * @author yyd
 * @since 2019-09-20
 */
@Service
public class SysPositionGroupAuthorizeServiceImpl extends ServiceImpl<SysPositionGroupAuthorizeMapper, SysPositionGroupAuthorize> implements SysPositionGroupAuthorizeService {

	@Autowired
	private SysPositionGroupAuthorizeMapper sysPositionGroupAuthorizeMapper;

	/**
	 * 权限管理-设置岗位组自动授权
	 */
	@Transactional
	@Override
	public int add(SaveSysPositionGroupAuthorizeReqParam param) {
		int num=0;
		//先删除
		for (String  positionGroupId : param.getDeletePositionGroupIds()) {
			QueryWrapper<SysPositionGroupAuthorize>qy=new QueryWrapper<>();
			qy.eq("position_group_id", positionGroupId);
			sysPositionGroupAuthorizeMapper.delete(qy);
		}
		//后添加
		for (String  positionGroupId : param.getSavePositionGroupIds()) {
			SysPositionGroupAuthorize bean=new SysPositionGroupAuthorize();
			bean.setPositionGroupId(positionGroupId);
			if(!StringUtils.isEmpty(positionGroupId)) {
				QueryWrapper<SysPositionGroupAuthorize>qy=new QueryWrapper<>();
				qy.eq("position_group_id", positionGroupId);
				SysPositionGroupAuthorize sp=sysPositionGroupAuthorizeMapper.selectOne(qy);
				if(null==sp) {
					bean.setId(UUIDUtils.create());
					num=sysPositionGroupAuthorizeMapper.insert(bean);
				}
			}
		}
		return num;
	}
	
}
